<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="LoginForm" :rules="rules" ref="loginFormRef">
          <h1 class="title">HELLO</h1>
          <h2 class="subTitle">欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="LoginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="LoginForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login_btn" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { useRouter, useRoute } from 'vue-router';
import { ElNotification } from 'element-plus'
import getHours from '@/utils/gethours'
const $router = useRouter()
const $route = useRoute()
const UserStore = useUserStore()
const LoginForm = reactive({
  username: 'admin',
  password: '111111',
})
const loginFormRef = ref()
const validateUsername = (rule: any, value: any, callback: any) => {
  const checkUserName = /^[\w\u4E00-\u9FA5]{4,20}$/;
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else if (!checkUserName.test(value)) {
    callback(new Error('用户名2-20位,只能包含汉字/数字/字母和下划线'))
  } else {
    callback()
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  const checkPassword = /^\d{1,9}$/
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else if (!checkPassword.test(value)) {
    callback(new Error('密码只能由数字组成'))
  } else {
    callback()
  }
}
const rules = reactive({
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }],
})
const login = async () => {
  await loginFormRef.value.validate()
  try {
    await UserStore.userLogin(LoginForm)
    $router.push({
      path: $route.query.redirect as string || '/home'
    })
    ElNotification({
      title: `'hi,${getHours()}'`,
      message: '欢迎回来',
      type: 'success',
    })
  } catch (error) {
    ElNotification({
      message: (error as Error).message,
      type: 'error',
    })
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('../../assets/images/background.jpg');
  background-size: cover;
  background-repeat: no-repeat;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background-image: url('../../assets/images/login_form.png');
    background-repeat: no-repeat;
    background-size: cover;
    padding: 20px;

    .title {
      font-size: 40px;
      color: white;
    }

    .subTitle {
      margin: 20px 0;
      font-size: 20px;
      color: white;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
